package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.db.domain.MarketFootprintExample;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminFootprintServiceImpl implements AdminFootprintService {
    @Override
    public List<MarketFootprint> list(final String page, final String limit, final String sort, final String order, final String userId, final String goodsId) {

        SqlSession session = MybatisUtils.getSession();
        MarketFootprintMapper marketFootprintMapper = session.getMapper(MarketFootprintMapper.class);
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
        }

        marketFootprintExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        session.commit();
        session.close();
        return marketFootprints;
    }
}
